package com.net.http;

import com.google.gson.JsonObject;

import okhttp3.*;
import java.io.*;
import java.util.*;

import com.google.gson.*;

import org.json.JSONException;
import org.json.JSONObject;

public class NetClient {
    private static String Post (Map<String, String> params, String url) {
        if(params == null || url == null) return null;
        OkHttpClient okHttpClient = new OkHttpClient();

        JSONObject json = new JSONObject();
        for (String key : params.keySet()) {
            try {
                json.put(key, params.get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        MediaType JSON = MediaType.parse("application/json;charset=utf-8");
        RequestBody body = RequestBody.create(JSON, String.valueOf(json));

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        Call call = okHttpClient.newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            return response == null ? null : response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String Get (String url) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url).build();

        Call call = okHttpClient.newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response == null ? null : response.body().string();
    }

    private static JsonObject stringToJSON(String str) {
        return new JsonParser().parse(str).getAsJsonObject();
    }

    public static JsonObject GetLogin(String username, String password) {
        String url = "https:////health.icys.top//session//login";
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("username", username);
        map.put("password", password);
        String str = Post(map, url);
        if(str != null) return stringToJSON(str);
        return null;
    }

    public static String GetInforStr(String token) {
        String url = "https:////health.icys.top//user//get-info";
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("token", token);
        String str = Post(map, url);
        return str;
    }

    public static JsonObject GetInfor(String token) {
        String url = "https:////health.icys.top//user//get-info";
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("token", token);
        String str = Post(map, url);
        if(str != null) return stringToJSON(str);
        return null;
    }

    public static JsonObject GetLogOut(String token) {
        String url = "https:////health.icys.top//session//logout";
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("token", token);
        String str = Post(map, url);
        if(str != null) return stringToJSON(str);
        return null;
    }

    public static JsonObject ChangePassword(String token, String oldPassword, String newPassword) {
        String url = "https:////health.icys.top//user//change-password";
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("token", token);
        map.put("old_password", oldPassword);
        map.put("new_password", newPassword);
        String str = Post(map, url);
        if(str != null) return stringToJSON(str);
        return null;
    }

    public static JsonObject AddPlace(String token, String place) {
        String url = "https:////health.icys.top//place//stay";
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("token", token);
        map.put("place", place);
        String str = Post(map, url);
        if (str != null) return stringToJSON(str);
        return null;
    }

    public static String GetUserByNfc (String nfc) throws IOException {
        String url = "http://47.98.60.14:8099/getUserById" + "/" + nfc;
        return Get(url);
    }
}
